package Datatypes;

import java.io.Serializable;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;

/**
* 
* Desarrollador: Jonathan Franco<br><br>
* 
* [public PDate(String sqldate);]<br>
* Crea un PDate a partir de un String proveniente de SQL.<br>
* Otro tipo de sintaxis crea problemas.<br><br>
* 
* [public PDate(Integer day, Integer month, Integer year, <br>Integer hour, Integer min, Integer sec);]<br>
* Crea un PDate a partir de los valores <br> ingresados DD-MM-YYYY-HH-MM-SS.<br>
* No se controla el ingreso de parametros erroneos.<br><br>
* 
* [public PDate(Integer day, Integer month, Integer year);]<br>
* Crea un PDate a partir de un de los valores ingresados DD-MM-YYYY.<br>
* No se controla el ingreso de parametros erroneos.<br>
* 
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "pDate", propOrder = {
    
    "day",
    "month",
    "year",
    "hour",
    "min",
    "sec"
    
})
public class PDate implements Serializable {
     
    private String day = null;
    private String month = null;
    private String year = null;
    private String hour = null;
    private String min = null;
    private String sec = null;

    public PDate() {}
    
    public PDate(String sqldate) throws Exception{
    
        //'2005-07-10 00:27:45'(SQL DATE FORMAT)
        try{
              
            if(!sqldate.contains(":")){

                this.year = sqldate.substring(0, sqldate.indexOf("-"));
                this.month = sqldate.substring(sqldate.indexOf("-") + 1, sqldate.lastIndexOf("-"));
                this.day = sqldate.substring(sqldate.lastIndexOf("-") + 1, sqldate.length());

            }

            else{

                this.year = sqldate.substring(0, sqldate.indexOf("-"));
                this.month = sqldate.substring(sqldate.indexOf("-") + 1, sqldate.lastIndexOf("-"));
                this.day = sqldate.substring(sqldate.lastIndexOf("-") + 1, sqldate.indexOf(" "));
                this.hour = sqldate.substring(sqldate.indexOf(" ")+ 1, sqldate.indexOf(":"));
                this.min = sqldate.substring(sqldate.indexOf(":") + 1, sqldate.lastIndexOf(":"));
                this.sec = sqldate.substring(sqldate.lastIndexOf(":") + 1, sqldate.length());

            }
            
        }
        
        catch(Exception e){
        
            throw new Exception("Error de formato SQLDATE: ('2005-07-10 00:27:45'(SQL DATE FORMAT))");
        
        }
    
    }
    
    public PDate(Integer day, Integer month, Integer year, Integer hour, Integer min, Integer sec){
    
        if(day < 10){
        
            this.day = "0" + day.toString();
        
        }
        
        else{
        
            this.day = day.toString();
            
        }
        
        if(month < 10){
        
            this.month = "0" + month.toString();
        
        }
        
        else{
        
            this.month = month.toString();
            
        }
        
        this.year = year.toString();
    
        if(hour < 10){
        
            this.hour = "0" + hour.toString();
        
        }
        
        else{
        
            this.hour = hour.toString();
            
        }
        
        if(min < 10){
        
            this.min = "0" + min.toString();
        
        }
        
        else{
        
            this.min = min.toString();
            
        }
        
        if(sec < 10){
        
            this.sec = "0" + sec.toString();
        
        }
        
        else{
        
            this.sec = sec.toString();
            
        }
        
    }
    
    public PDate(Integer day, Integer month, Integer year){
    
        if(day < 10){
        
            this.day = "0" + day.toString();
        
        }
        
        else{
        
            this.day = day.toString();
            
        }
        
        if(month < 10){
        
            this.month = "0" + month.toString();
        
        }
        
        else{
        
            this.month = month.toString();
            
        }
        
        this.year = year.toString();
    
    }

    public String getDay(){ return day; }
    public String getMonth(){ return month; }
    public String getYear(){ return year; }
    public String getHour(){ return hour; }
    public String getMin(){ return min; }
    public String getSec(){ return sec; }
    public void setDay(String day) { this.day = day; }
    public void setMonth(String month) { this.month = month; }
    public void setYear(String year) { this.year = year; }
    public void setHour(String hour) { this.hour = hour; }
    public void setMin(String min) { this.min = min; }
    public void setSec(String sec) { this.sec = sec; }
    
    /**
     * Desarrollador: Jonathan Franco<br>
     * Devuelve un string del date en formato SQL.
     * 
     * @return String 
     */
    public String getSQLDate(){
    
        //'2005-07-10 00:27:45'(SQL DATE FORMAT)
    
        String date;
        
        if((hour == null) && (min == null) && (sec == null)){
        
            date = year + "-" + month + "-" + day;
    
        }
        
        else{
        
            date = year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec;
        
        }
        
        return date;
            
    }
    
    /**
     * Desarrollador: Jonathan Franco<br>
     * Devuelve un string para mostrar el PDate.
     * 
     * @return String 
     */
    public String getString(){
    
        String date;
        
        if((hour == null) && (min == null) && (sec == null)){
        
            date = day + "/" + month + "/" + year;
    
        }
        
        else{
        
            date = day + "/" + month + "/" + year + " " + hour + ":" + min + ":" + sec;
        
        }
        
        return date;
    
    }
    
    @Override
    public String toString(){
    
        String date;
        
        if((hour == null) && (min == null) && (sec == null)){
        
            date = day + "/" + month + "/" + year;
    
        }
        
        else{
        
            date = day + "/" + month + "/" + year + " " + hour + ":" + min + ":" + sec;
        
        }
        
        return date;
    
    }
    
}
